package com.ecoolex.ying.space.core.pojo;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.util.crypto.MD5Util;

import java.io.Serializable;

public class Root implements Serializable {
    private ChildNode root;

    private int attachmentNumber;

    public ChildNode getRoot() {
        return root;
    }

    public Root setRoot(ChildNode root) {
        this.root = root;
        return this;
    }

    public String md5() {
        String json = JSON.toJSONString(this);
        return MD5Util.encrypt(json);
    }

    public synchronized int attachmentNumber() {
        attachmentNumber = 0;
        counting(root);
        return attachmentNumber;
    }

    private void counting(Node node) {
        if (node instanceof ChildNode) {
            ((ChildNode) node).getChildList().forEach(n -> counting(n));
        } else if (node instanceof AttachmentNode) {
            attachmentNumber++;
        }
    }

}
